﻿
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;

namespace Share.Services;

public interface IRequestGatewayService<T> where T : IcbcApiRequest
{
    Task<string> PostAsync(RequestBase<T> request);
}

public class RequestGatewayService<T>(ILogger<RequestGatewayService<T>> logger, IHttpClientFactory httpClientFactory) : IRequestGatewayService<T> where T : IcbcApiRequest
{
    public async Task<string> PostAsync(RequestBase<T> request)
    {
        using HttpClient client = httpClientFactory.CreateClient("ApiGateway" ?? "");
        var formContent = new FormUrlEncodedContent(request.GetPostValues());
        var response = await client.PostAsync(request.ApiAddress, formContent);
        //var result = await response.Content.ReadFromJsonAsync<ResponseBase>();
        //return result.ResponseBizContent.ReturnMsg;

        var result = await response.Content.ReadAsStringAsync();

        logger.LogInformation("接收到网关返回{result}\n", result);

        return result;
    }
}

